package com.lottery.ruok.lottrey.adapter;

import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.applozic.mobicomkit.contact.AppContactService;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.eventbusObject.AddChatUser;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.ResumeChatUserRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetBlockListResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.OperationSuccessResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import glideHelp.GlideHelper;
import utils.Utils;

/**
 * Created by ASUS on 2017/11/13.
 */

public class AddUserAdapter extends BaseAdapter {
    private GetBlockListResponse getBlockListResponse;
    private FragmentActivity mContext;
    private AppContactService appContactService;

    public AddUserAdapter(GetBlockListResponse getBlockListResponse, FragmentActivity context){
        this.getBlockListResponse = getBlockListResponse;
        mContext = context;
        appContactService = new AppContactService(context);
    }

    @Override
    public int getCount() {
        return getBlockListResponse.getList().size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.add_new_user_item, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RetrofitBase.getRetrofit().create(ResumeChatUserRequest.class)
                        .getResult(UserInfo.getToken(), getBlockListResponse.getList().get(i).getRy_id(), 1)
                        .compose(RxHelper.<OperationSuccessResponse>handleResult())
                        .subscribe(new RxSubscribe<OperationSuccessResponse>(mContext) {
                            @Override
                            protected void _onNext(OperationSuccessResponse operationSuccessResponse) {
                                Utils.showToast(operationSuccessResponse.getMsg());

                                getBlockListResponse.getList().remove(i);
                                notifyDataSetChanged();

                                EventBus.getDefault().post(new AddChatUser());
                            }

                            @Override
                            protected boolean showDialog() {
                                return true;
                            }
                        });
            }
        });
        if(getBlockListResponse.getList().get(i).getAvatar() == null){
            holder.avatar.setImageResource(R.mipmap.chat_default);
        } else {
            GlideHelper.loadImage(mContext, getBlockListResponse.getList().get(i).getAvatar(), holder.avatar);
        }
        holder.name.setText(appContactService.getContactById(getBlockListResponse.getList().get(i).getNickname()).getDisplayName());

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.avatar)
        CircleImageView avatar;
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.add)
        LinearLayout add;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
